package com.cancer.common.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cancer.common.util.ExceptionUtil;
import java.io.Serializable;
import java.util.function.Consumer;

public class APIModel implements Serializable {
    /**  **/
    private static final long serialVersionUID = 1L;
    private static Logger logger = LoggerFactory.getLogger(APIModel.class);

    public APIModel toError(String errorMsg) {
        return this.set(APIResult.ERROR.code, errorMsg, data);
    }

    public APIModel toError() {
        return this.set(APIResult.ERROR.code, "操作失败", data);
    }

    public boolean isSucc() {
        return this.getCode() == APIResult.OK.code;
    }




    /**
     * 消息lambda
     *
     * @param cons
     * @return
     * @author liujun May 8, 2017
     */
    public static APIModel handle(Consumer<APIModel> cons) {
        APIModel model = new APIModel();
        try {
            cons.accept(model);
        } catch (Exception e) {
            logger.error(ExceptionUtil.getStackTrace(e));
            model.set(APIResult.ERROR.getCode(), e.toString(), null);
        }
        return model;
    }

    private int code = APIResult.OK.code;
    private String msg = APIResult.OK.msg;
    private Object data;

    public APIModel() {
    }

    public APIModel(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public APIModel(APIModel model) {
        this.code = model.getCode();
        this.msg = model.getMsg();
        this.data = model.getData();
    }

    public APIModel(APIResult result) {
        this.code = result.getCode();
        this.msg = result.getMsg();
    }

    public APIModel set(APIModel model) {
        this.code = model.getCode();
        this.msg = model.getMsg();
        this.data = model.getData();
        return this;
    }

    public APIModel set(APIResult result) {
        this.code = result.getCode();
        this.msg = result.getMsg();
        return this;
    }
    public APIModel set(boolean code, String msg, Object data) {
        this.code = code ? APIResult.OK.code:APIResult.ERROR.code;
        this.msg = msg;
        this.data = data;
        return this;
    }
    public APIModel set(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        return this;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "APIModel [code=" + code + ", msg=" + msg + ", data=" + data + "]";
    }

}
